// 测试帮助函数
import { askAI } from '@/api/ai.js'
import { getUserConversations } from '@/api/history.js'
import { useChatStore } from '@/store/chat.js'

/**
 * 测试问答功能
 */
export const testQAFunction = async () => {
  console.log('=== 开始测试问答功能 ===')
  
  try {
    const testQuestion = '什么是RAG技术？请简单介绍一下。'
    
    const payload = {
      question: testQuestion,
      model: 'deepseek-chat',
      maxTokens: 1000,
      temperature: 0.7,
      saveRecord: true
    }
    
    console.log('发送测试问题:', payload)
    
    const response = await askAI(payload)
    
    console.log('收到回答:', response)
    
    if (response && response.data) {
      console.log('✅ 问答功能测试成功')
      console.log('会话ID:', response.data.sessionId)
      console.log('回答内容:', response.data.answer)
      return {
        success: true,
        sessionId: response.data.sessionId,
        answer: response.data.answer
      }
    } else {
      console.log('❌ 问答功能测试失败：响应数据异常')
      return { success: false, error: '响应数据异常' }
    }
    
  } catch (error) {
    console.error('❌ 问答功能测试失败:', error)
    return { success: false, error: error.message }
  }
}

/**
 * 测试历史记录功能
 */
export const testHistoryFunction = async () => {
  console.log('=== 开始测试历史记录功能 ===')
  
  try {
    const response = await getUserConversations({
      page: 1,
      pageSize: 10
    })
    
    console.log('历史记录响应:', response)
    
    if (response && response.data) {
      console.log('✅ 历史记录功能测试成功')
      console.log('记录数量:', response.data.total || response.data.length || 0)
      
      const conversations = response.data.conversations || response.data.records || []
      console.log('会话列表:', conversations)
      
      return {
        success: true,
        conversations: conversations,
        total: response.data.total || conversations.length
      }
    } else {
      console.log('❌ 历史记录功能测试失败：响应数据异常')
      return { success: false, error: '响应数据异常' }
    }
    
  } catch (error) {
    console.error('❌ 历史记录功能测试失败:', error)
    return { success: false, error: error.message }
  }
}

/**
 * 测试聊天store功能
 */
export const testChatStore = () => {
  console.log('=== 开始测试聊天store功能 ===')
  
  try {
    const chatStore = useChatStore()
    
    // 测试创建新对话
    const newChat = chatStore.createNewChat('测试对话')
    console.log('创建新对话:', newChat)
    
    // 测试添加消息
    const userMessage = chatStore.addMessage('user', '这是一条测试消息')
    console.log('添加用户消息:', userMessage)
    
    const assistantMessage = chatStore.addMessage('assistant', '这是AI的回复消息')
    console.log('添加AI消息:', assistantMessage)
    
    // 测试获取当前对话
    const currentChat = chatStore.getCurrentChat
    console.log('当前对话:', currentChat)
    
    // 测试清理空对话
    const cleanedCount = chatStore.removeEmptyChatsIfAny()
    console.log('清理空对话数量:', cleanedCount)
    
    // 测试保存到本地存储
    chatStore.saveToStorage()
    console.log('保存到本地存储完成')
    
    console.log('✅ 聊天store功能测试成功')
    return {
      success: true,
      chatId: currentChat.id,
      messageCount: currentChat.messages.length
    }
    
  } catch (error) {
    console.error('❌ 聊天store功能测试失败:', error)
    return { success: false, error: error.message }
  }
}

/**
 * 完整功能测试
 */
export const runFullTest = async () => {
  console.log('=== 开始完整功能测试 ===')
  
  const results = {
    qaTest: null,
    historyTest: null,
    storeTest: null,
    overall: false
  }
  
  // 测试聊天store
  results.storeTest = testChatStore()
  
  // 测试问答功能
  results.qaTest = await testQAFunction()
  
  // 等待一下，让记录保存
  await new Promise(resolve => setTimeout(resolve, 2000))
  
  // 测试历史记录功能
  results.historyTest = await testHistoryFunction()
  
  // 判断整体测试结果
  results.overall = results.qaTest.success && 
                   results.historyTest.success && 
                   results.storeTest.success
  
  console.log('=== 完整功能测试结果 ===')
  console.log('问答功能:', results.qaTest.success ? '✅ 成功' : '❌ 失败')
  console.log('历史记录功能:', results.historyTest.success ? '✅ 成功' : '❌ 失败')
  console.log('聊天store功能:', results.storeTest.success ? '✅ 成功' : '❌ 失败')
  console.log('整体功能:', results.overall ? '✅ 成功' : '❌ 失败')
  
  return results
}

/**
 * 在控制台显示测试按钮（开发调试用）
 */
export const showTestButtons = () => {
  if (typeof window !== 'undefined' && window.console) {
    console.log('=== 测试功能 ===')
    console.log('运行以下命令进行测试:')
    console.log('- window.testQA() // 测试问答功能')
    console.log('- window.testHistory() // 测试历史记录功能') 
    console.log('- window.testStore() // 测试聊天store功能')
    console.log('- window.runFullTest() // 运行完整测试')
    
    window.testQA = testQAFunction
    window.testHistory = testHistoryFunction
    window.testStore = testChatStore
    window.runFullTest = runFullTest
  }
} 